from datetime import datetime, timedelta
import redis
from master import config, settings
import json


def create_collection_name():
    collection_name = settings.start_date_str + '_' + settings.end_date_str
    if len(settings.must_keywords.split()) > 0:
        collection_name += '_' + '_'.join(settings.must_keywords.split(','))
    elif len(settings.included_keywords.split(',')) > 0:
        collection_name += '_' + '_'.join(settings.included_keywords.split(','))
    else:
        collection_name += ''
    return collection_name


def combine_keywords():
    keywords = ''
    if len(settings.must_keywords.strip()) > 0:
        keywords += ' '.join(settings.must_keywords.split(','))
    if len(settings.included_keywords.strip()) > 0:
        keywords += '%20OR%20'.join(settings.included_keywords.split(','))  # '('+ ')'+
    if len(settings.except_keywords.strip()) > 0:
        keywords += '-' + ' -'.join(settings.except_keywords.split(','))
    return keywords


def date_split(start, end, span):
    next_step = start
    date_range_list = []
    while True:
        date_range = []
        date_range.append(next_step)
        next_step = next_step + timedelta(days=span)
        if next_step > end:
            date_range.append(end + timedelta(days=1))
            date_range_list.append(date_range)
            break
        else:
            date_range.append(next_step)
        date_range_list.append(date_range)
    return date_range_list


def create_keyword_commands_list(start_date, end_date, day_span, latest=False):
    ori_url = settings.keywords_url
    date_range_list = date_split(start_date, end_date, day_span)
    commands_list = []
    processed_keywords = combine_keywords()
    for date_range in date_range_list:
        command = {}
        url = str.format(ori_url, processed_keywords, date_range[0].strftime('%Y-%m-%d'),
                         date_range[1].strftime('%Y-%m-%d'))
        referer = str.format(settings.referer_url, processed_keywords, date_range[0].strftime('%Y-%m-%d'),
                             date_range[1].strftime('%Y-%m-%d'))
        # if latest:
        #     url += '&f=live' 'csrf': '', 'guest': '',
        command['type'] = 'SEARCH'
        command['db'] = settings.target_db
        command['user_info'] = config.userinfo_collection
        command['collection'] = create_collection_name()
        command['url'] = url
        command['referer'] = referer
        command['start_date'] = '1000-01-01'
        commands_list.append(command)
    return commands_list


# 执行两种分发任务：一是按照日期&关键词分发（关键词查找），二是按照uid及截止日期分发（个人时间线获取及更新）
if __name__ == '__main__':
    # 接受日期参数并分解任务，放入redis任务队列（queue：task）

    start_date = None
    end_date = None

    try:
        start_date = datetime.strptime(settings.start_date_str, '%Y-%m-%d')
        end_date = datetime.strptime(settings.end_date_str, '%Y-%m-%d')
    except:
        print("请检查日期是否输入正确！")
        exit(0)
    if settings.day_span <= 0:
        print("日期间隔应大于0")
        exit(0)
    if start_date > end_date:
        print("起始日期应小于终止日期")
        exit(0)
    if len(settings.target_db) <= 0:
        print("未指定数据库")
        exit(0)

    commands_list = create_keyword_commands_list(start_date, end_date, settings.day_span, settings.latest)

    redis_client = redis.Redis(host=config.redis_host, port=config.redis_port)
    if settings.clear_exist_cmd:
        redis_client.delete(config.task_queue_name)
    for command in commands_list:
        t = json.dumps(command)
        redis_client.lpush(config.task_queue_name, t)
        print(command)
    print(redis_client.llen(config.task_queue_name))
    print("任务已发送到redis队列！")
